package com.hust.hui.quicksilver.alarm;


import com.hust.hui.quicksilver.alarm.entity.AlarmLevelEnum;
import com.hust.hui.quicksilver.alarm.execut.*;

/**
 * Created by yihui on 2017/4/19.
 */
public class SimpleExecuteFactory {

    private volatile static IExecute defaultNoneExecute;

    private volatile static IExecute defaultEmailExecute;

    private volatile static IExecute defaultWeiXinExecute;

    private volatile static IExecute defaultSmsExecute;

    private volatile static IExecute defaultLogExecute;


    private static IExecute getDefaultEmailExecute() {
        if (defaultEmailExecute == null) {
            synchronized (SimpleExecuteFactory.class) {
                if (defaultEmailExecute == null) {
                    defaultEmailExecute = new EmailExecute();
                }
            }
        }

        return defaultEmailExecute;
    }


    private static IExecute getDefaultWeiXinExecute() {
        if (defaultWeiXinExecute == null) {
            synchronized (SimpleExecuteFactory.class) {
                if (defaultWeiXinExecute == null) {
                    defaultWeiXinExecute = new WeiXinExecute();
                }
            }
        }

        return defaultWeiXinExecute;
    }


    private static IExecute getDefaultSmsExecute() {
        if (defaultSmsExecute == null) {
            synchronized (SimpleExecuteFactory.class) {
                if (defaultSmsExecute == null) {
                    defaultSmsExecute = new SmsExecute();
                }
            }
        }

        return defaultSmsExecute;
    }


    private static IExecute getDefaultLogExecute() {
        if (defaultLogExecute == null) {
            synchronized (SimpleExecuteFactory.class) {
                if (defaultLogExecute == null) {
                    defaultLogExecute = new LogExecute();
                }
            }
        }

        return defaultLogExecute;
    }


    private static IExecute getDefaultNoneExecute() {
        if (defaultNoneExecute == null) {
            synchronized (SimpleExecuteFactory.class) {
                if (defaultNoneExecute == null) {
                    defaultNoneExecute = new NoneExecute();
                }
            }
        }

        return defaultNoneExecute;
    }


    public static IExecute getExecute(AlarmLevelEnum alarmLevel) {
        switch (alarmLevel) {
            case EMAIL:
                return getDefaultEmailExecute();
            case WEIXIN:
                return getDefaultWeiXinExecute();
            case SMS:
                return getDefaultSmsExecute();
            case LOG:
                return getDefaultLogExecute();
            case NONE:
                return getDefaultNoneExecute();
            default:
                throw new IllegalStateException("no executor for " + alarmLevel);
        }
    }
}
